package com.meetingshow.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.meetingshow.commu.jsonbean.AnswerJsonBean;
import com.meetingshow.commu.jsonbean.QuestJsonBean;
import com.meetingshow.commu.uiservice.model.AnswerVO;
import com.meetingshow.commu.uiservice.model.QuestionVO;
import com.meetingshow.meeting.jsonbean.MeetingJsonBean;
import com.meetingshow.meeting.jsonbean.TopicJsonBean;
import com.meetingshow.meeting.uiservice.model.MeetingVO;
import com.meetingshow.meeting.uiservice.model.TopicVO;
import com.meetingshow.privilege.jsonbean.PriUserFollowJsonBean;
import com.meetingshow.privilege.jsonbean.UserJsonBean;
import com.meetingshow.privilege.uiservice.model.PriUserFollowVO;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.weibo.jsonbean.WeiboJsonBean;
import com.meetingshow.weibo.uiservice.model.WeiboVO;

public class JSONUtils {
    public static AnswerJsonBean toAnswerJsonBean(AnswerVO answerVO) {
        if (answerVO == null)
            return null;
        AnswerJsonBean answerJsonBean = new AnswerJsonBean();
        if (answerVO.getAnswerId() != null)
            answerJsonBean.setAnswerId(String.valueOf(answerVO.getAnswerId()));

        answerJsonBean.setContent(answerVO.getContent());

        if (answerVO.getFromUserId() != null)
            answerJsonBean.setFromUserId(String.valueOf(answerVO
                    .getFromUserId()));

        answerJsonBean.setFromUserName(answerVO.getFromUserName());

        if (answerVO.getQuestion() != null
                && answerVO.getQuestion().getQuestionId() != null)
            answerJsonBean.setQuestionId(String.valueOf(answerVO.getQuestion()
                    .getQuestionId()));

        if (answerVO.getTopicId() != null)
            answerJsonBean.setTopicId(String.valueOf(answerVO.getTopicId()));

        answerJsonBean.setTopicName(answerVO.getTopicName());

        if (answerVO.getToUserId() != null)
            answerJsonBean.setToUserId(String.valueOf(answerVO.getToUserId()));

        answerJsonBean.setToUserName(answerVO.getToUserName());

        if (answerVO.getCreateTime() != null)
            answerJsonBean.setCreateTime(DateFormatUtils.format(
                    answerVO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));

        answerJsonBean.setToWeiboName(answerVO.getToWeiboName());
        answerJsonBean.setStatusId(answerVO.getStatusId());

        return answerJsonBean;
    }

    public static AnswerVO toAnswerVO(AnswerJsonBean answerJsonBean) {
        if (answerJsonBean == null)
            return null;

        AnswerVO answerVO = new AnswerVO();

        if (StringUtils.isNotBlank(answerJsonBean.getAnswerId()))
            answerVO.setAnswerId(Integer.valueOf(answerJsonBean.getAnswerId()));

        answerVO.setContent(answerJsonBean.getContent());

        if (StringUtils.isNotBlank(answerJsonBean.getFromUserId()))
            answerVO.setFromUserId(Integer.valueOf(answerJsonBean
                    .getFromUserId()));

        answerVO.setFromUserName(answerJsonBean.getFromUserName());

        if (StringUtils.isNotBlank(answerJsonBean.getQuestionId())) {
            QuestionVO question = new QuestionVO();
            question.setQuestionId(Integer.valueOf(answerJsonBean
                    .getQuestionId()));
            answerVO.setQuestion(question);
        }

        if (StringUtils.isNotBlank(answerJsonBean.getTopicId()))
            answerVO.setTopicId(Integer.valueOf(answerJsonBean.getTopicId()));

        answerVO.setTopicName(answerJsonBean.getTopicName());

        if (StringUtils.isNotBlank(answerJsonBean.getToUserId()))
            answerVO.setToUserId(Integer.valueOf(answerJsonBean.getToUserId()));

        answerVO.setToUserName(answerJsonBean.getToUserName());
        answerVO.setToWeiboName(answerJsonBean.getToWeiboName());
        answerVO.setStatusId(answerJsonBean.getStatusId());

        return answerVO;
    }

    public static QuestJsonBean toQuestJsonBean(QuestionVO questionVO) {
        if (questionVO == null)
            return null;

        QuestJsonBean questJsonBean = new QuestJsonBean();

        questJsonBean.setContent(questionVO.getContent());

        if (questionVO.getFromUserId() != null)
            questJsonBean.setFromUserId(String.valueOf(questionVO
                    .getFromUserId()));

        questJsonBean.setFromUserName(questionVO.getFromUserName());

        if (questionVO.getQuestionId() != null)
            questJsonBean.setQuestionId(String.valueOf(questionVO
                    .getQuestionId()));

        if (questionVO.getTopicId() != null)
            questJsonBean.setTopicId(String.valueOf(questionVO.getTopicId()));

        questJsonBean.setTopicName(questionVO.getTopicName());

        if (questionVO.getToUserId() != null)
            questJsonBean.setToUserId(String.valueOf(questionVO.getToUserId()));

        questJsonBean.setToUserName(questionVO.getToUserName());

        if (questionVO.getCreateTime() != null)
            questJsonBean.setCreateTime(DateFormatUtils.format(
                    questionVO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));

        questJsonBean.setToWeiboName(questionVO.getToWeiboName());
        questJsonBean.setStatusId(questionVO.getStatusId());

        return questJsonBean;
    }

    public static QuestionVO toQuestionVO(QuestJsonBean questJsonBean) {
        if (questJsonBean == null)
            return null;

        QuestionVO questionVO = new QuestionVO();

        questionVO.setContent(questJsonBean.getContent());

        if (StringUtils.isNotBlank(questJsonBean.getFromUserId()))
            questionVO.setFromUserId(Integer.valueOf(questJsonBean
                    .getFromUserId()));

        questionVO.setFromUserName(questJsonBean.getFromUserName());

        if (StringUtils.isNotBlank(questJsonBean.getQuestionId()))
            questionVO.setQuestionId(Integer.valueOf(questJsonBean
                    .getQuestionId()));

        if (StringUtils.isNotBlank(questJsonBean.getTopicId()))
            questionVO.setTopicId(Integer.valueOf(questJsonBean.getTopicId()));

        questionVO.setTopicName(questJsonBean.getTopicName());

        if (StringUtils.isNotBlank(questJsonBean.getToUserId()))
            questionVO
                    .setToUserId(Integer.valueOf(questJsonBean.getToUserId()));

        questionVO.setToUserName(questJsonBean.getToUserName());
        questionVO.setToWeiboName(questJsonBean.getToWeiboName());
        questionVO.setStatusId(questJsonBean.getStatusId());

        return questionVO;
    }

    public static MeetingJsonBean toMeetingJsonBean(MeetingVO meetingVO) {
        if (meetingVO == null)
            return null;

        MeetingJsonBean mJsonBean = new MeetingJsonBean();

        mJsonBean.setAbstracT(meetingVO.getAbstracT());

        if (meetingVO.getCity() != null)
            mJsonBean.setCity(meetingVO.getCity().getName());

        if (meetingVO.getEndDate() != null)
            mJsonBean.setEndDate(DateFormatUtils.format(meetingVO.getEndDate(),
                    "yyyy-MM-dd HH:mm:ss"));

        if (meetingVO.getMeetingId() != null)
            mJsonBean.setMeetId(String.valueOf(meetingVO.getMeetingId()));

        mJsonBean.setName(meetingVO.getName());
        mJsonBean.setNameEn(meetingVO.getNameEn());

        if (meetingVO.getStartDate() != null)
            mJsonBean.setStartDate(DateFormatUtils.format(
                    meetingVO.getStartDate(), "yyyy-MM-dd HH:mm:ss"));
        
        if (meetingVO.getCharge() != null)
            mJsonBean.setCharge(String.valueOf(meetingVO.getCharge()));
        
        if (meetingVO.getCity() != null && meetingVO.getCity().getCityId() != null)
            mJsonBean.setCityId(String.valueOf(meetingVO.getCity().getCityId()));
        
        mJsonBean.setContactFax(meetingVO.getContactFax());
        mJsonBean.setContactor(meetingVO.getContactor());
        mJsonBean.setContactPhone(meetingVO.getContactPhone());
        mJsonBean.setCoOrganizer(meetingVO.getCoOrganizer());
        
        if (meetingVO.getCreateTime() != null)
            mJsonBean.setCreateTime(DateFormatUtils.format(meetingVO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        
        mJsonBean.setEmail(meetingVO.getEmail());
        
        if (meetingVO.getHot() != null)
            mJsonBean.setHot(String.valueOf(meetingVO.getHot()));
        
        mJsonBean.setImage(meetingVO.getImage());
        
        if (meetingVO.getIndustry() != null) {
            mJsonBean.setIndustry(meetingVO.getIndustry().getName());
            
            if (meetingVO.getIndustry().getIndustryId() != null)
                mJsonBean.setIndustryId(String.valueOf(meetingVO.getIndustry().getIndustryId()));
        }
        
        mJsonBean.setLocation(meetingVO.getLocation());
        
        if (meetingVO.getMeetingType() != null) {
            mJsonBean.setMeetingType(meetingVO.getMeetingType().getTypeName());
            if (meetingVO.getMeetingType().getTypeId() != null)
                mJsonBean.setMeetingTypeId(String.valueOf(meetingVO.getMeetingType().getTypeId()));
        }
        
        mJsonBean.setOrganizer(meetingVO.getOrganizer());
        if (meetingVO.getRecommend() != null)
            mJsonBean.setRecommend(String.valueOf(meetingVO.getRecommend()));
        
        return mJsonBean;
    }

    public static TopicJsonBean toTopicJsonBean(TopicVO topicVO) {
        if (topicVO == null)
            return null;

        TopicJsonBean topicJsonBean = new TopicJsonBean();

        topicJsonBean.setTopicDesc(topicVO.getTopicDesc());

        if (topicVO.getTopicId() != null)
            topicJsonBean.setTopicId(String.valueOf(topicVO.getTopicId()));

        topicJsonBean.setTopicName(topicVO.getTopicName());
        return topicJsonBean;
    }

    public static UserJsonBean toUserJsonBean(UserVO userVO) {
        if (userVO == null)
            return null;

        UserJsonBean uJsonBean = new UserJsonBean();

        uJsonBean.setEmail(userVO.getEmail());
        uJsonBean.setPassword(userVO.getPassword());

        if (userVO.getUserId() != null)
            uJsonBean.setUserId(String.valueOf(userVO.getUserId()));

        uJsonBean.setUserName(userVO.getUserName());
        uJsonBean.setUserType(userVO.getUserType());

        return uJsonBean;
    }

    public static UserVO toUserVO(UserJsonBean uJsonBean) {
        if (uJsonBean == null)
            return null;

        UserVO userVO = new UserVO();
        if (StringUtils.isNotBlank(uJsonBean.getUserId()))
            userVO.setUserId(Integer.valueOf(uJsonBean.getUserId()));

        userVO.setEmail(uJsonBean.getEmail());
        userVO.setPassword(uJsonBean.getPassword());
        userVO.setUserName(uJsonBean.getUserName());
        userVO.setUserType(uJsonBean.getUserType());

        return userVO;
    }

    public static WeiboVO toWeiboVO(WeiboJsonBean wJsonBean) {
        if (wJsonBean == null)
            return null;

        WeiboVO weiboVO = new WeiboVO();

        if (StringUtils.isNotBlank(wJsonBean.getId()))
            weiboVO.setId(Integer.valueOf(wJsonBean.getId()));

        weiboVO.setAccessToken(wJsonBean.getAccessToken());
        weiboVO.setAccessTokenSecret(wJsonBean.getAccessTokenSecret());

        if (StringUtils.isNotBlank(wJsonBean.getUserId()))
            weiboVO.setUserId(Integer.valueOf(wJsonBean.getUserId()));

        weiboVO.setWeiboId(wJsonBean.getWeiboId());
        weiboVO.setWeiboType(wJsonBean.getWeiboType());
        weiboVO.setVerifier(wJsonBean.getVerifier());

        return weiboVO;
    }

    public static WeiboJsonBean toWeiboJsonBean(WeiboVO weiboVO) {
        if (weiboVO == null)
            return null;

        WeiboJsonBean wJsonBean = new WeiboJsonBean();

        if (weiboVO.getId() != null)
            wJsonBean.setId(String.valueOf(weiboVO.getId()));

        wJsonBean.setAccessToken(weiboVO.getAccessToken());
        wJsonBean.setAccessTokenSecret(weiboVO.getAccessTokenSecret());

        if (weiboVO.getUserId() != null)
            wJsonBean.setUserId(String.valueOf(weiboVO.getUserId()));

        wJsonBean.setWeiboId(weiboVO.getWeiboId());
        wJsonBean.setWeiboType(weiboVO.getWeiboType());
        wJsonBean.setVerifier(weiboVO.getVerifier());

        return wJsonBean;
    }

    public static PriUserFollowVO toPriUserFollowVO(
            PriUserFollowJsonBean priUserFollowJsonBean) {
        if (priUserFollowJsonBean == null)
            return null;

        PriUserFollowVO priUserFollowVO = new PriUserFollowVO();

        if (StringUtils.isNotBlank(priUserFollowJsonBean.getId()))
            priUserFollowVO
                    .setId(Integer.valueOf(priUserFollowJsonBean.getId()));

        if (StringUtils.isNotBlank(priUserFollowJsonBean.getFromUserId()))
            priUserFollowVO.setFromUserId(Integer.valueOf(priUserFollowJsonBean
                    .getFromUserName()));

        priUserFollowVO
                .setFromUserName(priUserFollowJsonBean.getFromUserName());

        if (StringUtils.isNotBlank(priUserFollowJsonBean.getToUserId()))
            priUserFollowVO.setToUserId(Integer.valueOf(priUserFollowJsonBean
                    .getToUserId()));

        priUserFollowVO
                .setFromUserName(priUserFollowJsonBean.getFromUserName());

        return priUserFollowVO;
    }

    public static PriUserFollowJsonBean toPriUserFollowJsonBean(
            PriUserFollowVO priUserFollowVO) {
        if (priUserFollowVO == null)
            return null;

        PriUserFollowJsonBean priUserFollowJsonBean = new PriUserFollowJsonBean();

        if (priUserFollowVO.getId() != null)
            priUserFollowJsonBean
                    .setId(String.valueOf(priUserFollowVO.getId()));

        if (priUserFollowVO.getFromUserId() != null)
            priUserFollowJsonBean.setFromUserId(String.valueOf(priUserFollowVO
                    .getFromUserName()));

        priUserFollowJsonBean
                .setFromUserName(priUserFollowVO.getFromUserName());

        if (priUserFollowVO.getToUserId() != null)
            priUserFollowJsonBean.setToUserId(String.valueOf(priUserFollowVO
                    .getToUserId()));

        priUserFollowJsonBean
                .setFromUserName(priUserFollowVO.getFromUserName());

        if (priUserFollowVO.getCreateTime() != null)
            priUserFollowJsonBean.setCreateTime(DateFormatUtils.format(
                    priUserFollowVO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));

        return priUserFollowJsonBean;
    }
}
